package com.wx.mall.goods.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wx.mall.common.api.PageVo;
import com.wx.mall.common.utils.DataResult;
import com.wx.mall.goods.entity.Template;
import com.wx.mall.goods.service.ITemplateService;
import com.wx.mall.goods.vo.req.TemplateUpdateReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 模板表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/template")
@Api(tags = "模板相关接口")
public class TemplateController {

    @Autowired
    private ITemplateService proTemplateService;


    @PostMapping("/templates")
    @ApiOperation("分页获取模板数据")
//    @Valid PageVo vo
    public DataResult pageInfo(@Valid PageVo vo) {
        Page<Template> templatePage = proTemplateService.pageInfo(vo);
        return DataResult.success(templatePage);
    }

    @GetMapping("/templates")
    @ApiOperation("查询所有模板数据")
    public DataResult findAll() {
        List<Template> list = proTemplateService.list();
        return DataResult.success(list);
    }

    @GetMapping("/template/{id}")
    @ApiOperation("根据id查询数据")
    public DataResult findById(@ApiParam("id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        Template byId = proTemplateService.getById(id);
        return DataResult.success(byId);
    }

    @DeleteMapping("/template/{id}")
    @ApiOperation("根据id删除")
    public DataResult deleteById(@ApiParam("id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        proTemplateService.deleteById(id);
        return DataResult.success();
    }

    @PutMapping("/template")
    @ApiOperation("根据id编辑")
    public DataResult updateById(@Valid TemplateUpdateReqVO updateReqVO) {
        proTemplateService.updateById(updateReqVO);
        return DataResult.success();
    }

    @PostMapping("/template")
    @ApiOperation("新增")
    public DataResult add(@NotBlank(message = "模板名称不能为空") @ApiParam(value = "模板名称") String name) {
        proTemplateService.add(name);
        return DataResult.success();
    }


}

